import java.io.IOException;

public class EZParser {

	private static final String COMMAND_ADD = "ADD";
	private static final String COMMAND_DELETE = "DELETE";
	private static final String COMMAND_GOTO = "GOTO";
	private static final String COMMAND_EDIT = "EDIT";
	private static final String COMMAND_UNDO = "UNDO";

	public Object[] parseUserInput(String userInput) {

		String[] parts = userInput.split(";");

		if (parts[0].startsWith(COMMAND_ADD)) {
			return partsForAdd(parts);
		} else if (userInput.startsWith(COMMAND_EDIT)) {
			return partsForEdit(parts);
		} else if (userInput.startsWith(COMMAND_DELETE)) {
			return partsForRemove(parts);
		} else if (userInput.startsWith(COMMAND_GOTO)) {
			return partsForGoto(parts);
		} else if (userInput.startsWith(COMMAND_UNDO)) {
		    partsForUndo();
		} 

		return null;
	}

	private Object[] partsForAdd(String[] parts) {
		String[] inputs = parts[0].split(" ", 2);
		String command = inputs[0];
		String desc = inputs[1];
		Object[] returnArray = null;

		if (parts.length == 3) {
			returnArray = new Object[5];
			int date = Integer.parseInt(parts[1]);
			int time = Integer.parseInt(parts[2]);
			returnArray[2] = true; // timed
			returnArray[3] = date;
			returnArray[4] = time;
		}

		else if (parts.length == 2) {
			returnArray = new Object[4];
			int prio = Integer.parseInt(parts[1]);
			returnArray[2] = false; // not timed
			returnArray[3] = prio;
		}

		returnArray[0] = command;
		returnArray[1] = desc;
		return returnArray; // return exceptions if somethings wrong
	}

	private Object[] partsForEdit(String[] parts) {

		String[] inputs = parts[0].split(" ", 2);
		String command = inputs[0];
		int id = Integer.parseInt(inputs[1]);
		String desc = parts[1];
		Object[] returnArray = null;

		if (parts.length == 4) {
			returnArray = new Object[6];
			int date = Integer.parseInt(parts[2]);
			int time = Integer.parseInt(parts[3]);
			returnArray[3] = true; // timed
			returnArray[4] = date;
			returnArray[5] = time;
		}

		else if (parts.length == 3) {
			returnArray = new Object[5];
			int prio = Integer.parseInt(parts[2]);
			returnArray[3] = false; // not timed
			returnArray[4] = prio;
		}
		returnArray[0] = command;
		returnArray[1] = id;
		returnArray[2] = desc;
		return returnArray; // return exce if somethings wrong
	}

	private static Object[] partsForRemove(String[] parts) {

		String[] inputs = parts[0].split(" ", 3);

		String command = inputs[0];
		String type = (String) inputs[1];
		int id = Integer.parseInt(inputs[2]);

		Object returnArray[] = new Object[3];
		returnArray[0] = command;
		returnArray[1] = type;
		returnArray[2] = id;

		return returnArray;
	}

	private static Object[] partsForGoto(String[] parts) {

		String[] inputs = parts[0].split(" ", 3);

		String command = inputs[0];
		String type = inputs[1];
		int value = Integer.parseInt(inputs[2]);

		Object[] returnArray = new Object[3];
		returnArray[0] = command;
		returnArray[1] = type;

		if (value > 1000) {
			int date = value;
			returnArray[2] = date;
		} else {
			int prio = value;
			returnArray[2] = prio;
		}

		return returnArray;
	}
	
	public static String partsForUndo() {
		return "Undo has not been implemented yet.";
	}
}